package com.lingyi.algorithm.lab3;

import java.util.Arrays;

/**
 * 顺序栈实现
 * @author chenweilong
 * @email 1433471850@qq.com
 * @date 2020-08-20 14:08
 */
public class SqStack<T> {


    private int base;

    private int top;

    private Object[] stack;

    private int size;


    public SqStack(int size) {
        this.base = 0;
        this.top = 0;
        stack = new Object[size];
        this.size = size;
    }



    public void push(T t) {
//        1.判断栈是否满了
        if ((this.top - this.base) >= size) {
            int newLen = this.size * 2;
            stack = Arrays.copyOf(stack,newLen);
            this.size = newLen;
        }
        stack[top] = t;
        top++;
    }



    public T pop() {
        if (this.top == this.base) {
            System.out.println("栈是空的");
            return null;
        }
        return(T)stack[--top];
    }

    public void print() {
        System.out.println("~~~~~~~~~~~目前栈中的元素~~~~~~~~~~~");
        for (int i = this.base; i < this.top; i++) {
            System.out.println(stack[i]);
        }
    }

}
